package com.tsd.base.service.impl;

import com.tsd.base.dao.BaseExpressOrgMapper;
import com.tsd.base.entity.BaseExpressOrg;
import com.tsd.base.entity.BaseExpressOrgExt;
import com.tsd.base.entity.BaseExpressOrgItemExt;
import com.tsd.base.entity.BaseExpressOrgRateExt;
import com.tsd.base.service.BaseExpressOrgItemService;
import com.tsd.base.service.BaseExpressOrgRateService;
import com.tsd.base.service.BaseExpressOrgService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseExpressOrg 服务实现层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Service
public class BaseExpressOrgServiceImpl extends BaseServiceImpl implements BaseExpressOrgService {

    @Resource
    private BaseExpressOrgMapper baseExpressOrgMapper;
    @Resource
    private BaseExpressOrgItemService baseExpressOrgItemService;
    @Resource
    private BaseExpressOrgRateService baseExpressOrgRateService;

    @Override
    public BaseExpressOrg getBaseExpressOrgById(Long id) throws Exception {
        return baseExpressOrgMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseExpressOrgExt queryBySid(String sid) throws Exception {
        return baseExpressOrgMapper.selectBySid(sid);
    }

    @Override
    public void deleteBaseExpressOrg(Long id, SysUser opUser) throws Exception {
        BaseExpressOrg old = baseExpressOrgMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        baseExpressOrgMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void batchDeleteExpress(List<Long> ids, SysUser opUser) throws Exception {
        for (Long id : ids) {
            BaseExpressOrg old = baseExpressOrgMapper.selectByPrimaryKey(id);
            super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
            baseExpressOrgMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveBaseExpressOrg(BaseExpressOrgExt record, SysUserExt opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            //判断是否存在相同的编码，如存在不允许添加
            BaseExpressOrg searchExpress = new BaseExpressOrg();
            searchExpress.setCode(record.getCode());
            searchExpress.setDr(0);
            List<BaseExpressOrgExt> baseExpressOrgExtList = baseExpressOrgMapper.selectByEntity(searchExpress);
            if (!HlpUtils.isEmptyList(baseExpressOrgExtList)) {
                throw new HlpException("存在相同的编码，请重新输入");
            }
            baseExpressOrgMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            baseExpressOrgMapper.updateByPrimaryKeySelective(record);
        }
        baseExpressOrgItemService.saveBaseExpressOrgItem(record, opUser);
        baseExpressOrgRateService.saveBaseExpressOrgRate(record, opUser);
    }

    @Override
    public List<BaseExpressOrgExt> findBaseExpressOrgs(Map<String, Object> params, PageBean page) throws Exception {
        List<BaseExpressOrgExt> list;
        if (page == null) {
            list = baseExpressOrgMapper.selectByParams(params);
        } else {
            list = baseExpressOrgMapper.selectByPage(params, page);
        }
        if (!HlpUtils.isEmptyList(list) && params.containsKey("with_item")) {
            List<String> expressList = ListUtil.map(list, BaseExpressOrgExt::getSid);
            BaseExpressOrgItemExt itemParams = new BaseExpressOrgItemExt();
            itemParams.setDr(0);
            itemParams.setExpress_list(expressList);
            List<BaseExpressOrgItemExt> itemExtList = baseExpressOrgItemService.queryByEntity(itemParams);

            BaseExpressOrgRateExt rateParams = new BaseExpressOrgRateExt();
            rateParams.setDr(0);
            rateParams.setExpress_list(expressList);
            List<BaseExpressOrgRateExt> rateExtList = baseExpressOrgRateService.queryByEntity(rateParams);

            for (BaseExpressOrgExt orgExt : list) {
                List<BaseExpressOrgItemExt> filterItemList = ListUtil.filter(itemExtList, t -> orgExt.getSid().equals(t.getExpress_sid()));
                orgExt.setItem_list(filterItemList);
                List<BaseExpressOrgRateExt> filterRateList = ListUtil.filter(rateExtList, t -> orgExt.getSid().equals(t.getExpress_sid()));
                orgExt.setRate_list(filterRateList);
            }
        }
        return list;
    }

    @Override
    public List<BaseExpressOrgExt> findBaseExpressOrgs(Map<String, Object> params) throws Exception {
        return baseExpressOrgMapper.selectByParams(params);
    }
}
